﻿/// <reference path="RepeaterPageButton.js" />

var ____a;







var $RepeaterPager = TA.Turbo.Controls.RepeaterPager = function RepeaterPager(element, repeater)
{
	this.EventSender();

	this._element = element;
	this._repeater = repeater;
	this._pageButtons = new Array();
	this._allButtons = new Array();
	this._visible = this._element.style["display"] == "none";
}
$RepeaterPager.Inherit(EventSender);

if (____a)
{
	$RepeaterPager.prototype = new EventSender();
	$RepeaterPager.prototype.EventSender = new EventSender();

	$RepeaterPager.prototype._visible = true;
	$RepeaterPager.prototype._repeater = null;
	$RepeaterPager.prototype._element = document.createElement("asd");
	$RepeaterPager.prototype._pageButtonsElement = document.createElement("asd");
	$RepeaterPager.prototype._first = new $RepeaterPageButtonFirst();
	$RepeaterPager.prototype._previous = new $RepeaterPageButtonPrevious();
	$RepeaterPager.prototype._previousGroup = new $RepeaterPageButtonPrevious();
	$RepeaterPager.prototype._nextGroup = new $RepeaterPageButtonNext();
	$RepeaterPager.prototype._next = new $RepeaterPageButtonNext();
	$RepeaterPager.prototype._last = new $RepeaterPageButtonLast();
	$RepeaterPager.prototype._pageButtons = new Array();
	$RepeaterPager.prototype._allButtons = new Array();
}


$RepeaterPager.prototype.Init = function Init()
{
	var c = this._element.childNodes, cl = c.length;
	for (var iButton = 0; iButton < cl; ++iButton)
	{
		var elem = c[iButton];
		switch (elem.className)
		{
			case "first":
				this._first = new $RepeaterPageButtonFirst(elem, this._repeater);
				this._AddButton(this._first);
				break;
			case "previous":
				this._previous = new $RepeaterPageButtonPrevious(elem, this._repeater);
				this._AddButton(this._previous);
				break;
			case "previous_group":
				this._previousGroup = new $RepeaterPageButtonPreviousGroup(elem, this._repeater);
				this._AddButton(this._previousGroup);
				break;
			case "next_group":
				this._nextGroup = new $RepeaterPageButtonNextGroup(elem, this._repeater);
				this._AddButton(this._nextGroup);
				break;
			case "next":
				this._next = new $RepeaterPageButtonNext(elem, this._repeater);
				this._AddButton(this._next);
				break;
			case "last":
				this._last = new $RepeaterPageButtonLast(elem, this._repeater);
				this._AddButton(this._last);
				break;
			case "pages":
				this._pageButtonsElement = elem;
				var p = this._pageButtonsElement.childNodes, pl = p.length;
				for (var iPage = 0; iPage < pl; ++iPage)
				{
					var btn = new $RepeaterPageButtonNormal(p[iPage], this._repeater);
					this._pageButtons.push(btn);
					this._AddButton(btn);
				}
				break;
		}
	}

}
$RepeaterPager.prototype.get_Visible = function get_Visible()
{
	return this._visible;
}
$RepeaterPager.prototype.set_Visible = function set_Visible(value)
{
	this._visible = value;
	this._element.style["display"] = this._visible ? "" : "none";
}

$RepeaterPager.prototype._AddButton = function _AddButton(button)
{
	this._allButtons.push(button);

	button.AddEvent("GetPagerSettings", this.button_GetPagerSettings, this);
	button.AddEvent("GetNumPages", this.button_GetNumPages, this);
	button.AddEvent("GetCurrentPageIndex", this.button_GetCurrentPageIndex, this);
	button.AddEvent("PageIndexChanged", this.button_PageIndexChanged, this);
}
$RepeaterPager.prototype.button_GetPagerSettings = function button_GetPagerSettings(e)
{
	this.RaiseEvent("GetPagerSettings", e);
}
$RepeaterPager.prototype.button_GetNumPages = function button_GetNumPages(e)
{
	this.RaiseEvent("GetNumPages", e);
}
$RepeaterPager.prototype.button_GetCurrentPageIndex = function button_GetCurrentPageIndex(e)
{
	this.RaiseEvent("GetCurrentPageIndex", e);
}
$RepeaterPager.prototype.button_PageIndexChanged = function button_PageIndexChanged(e)
{
	this.RaiseEvent("PageIndexChanged", e);
}
$RepeaterPager.prototype.UpdateStatus = function UpdateStatus()
{
	for (var iButton = 0; iButton < this._allButtons.length; ++iButton)
	{
		this._allButtons[iButton].UpdateStatus();
	}
}
$RepeaterPager.prototype.UpdateStyle = function UpdateStyle()
{
	var numVisible = 0, pagesRange = this._repeater.get_PagesRange();
	for (var iPB = 0; iPB < this._pageButtons.length; ++iPB)
	{
		var pb = this._pageButtons[iPB];
		if (____a)
			pb = new $RepeaterPageButtonNormal();

		if (numVisible < pagesRange.End - pagesRange.Start)
		{
			if (!pb.get_Visible())
			{
				pb.set_Visible(true);
			}
			pb.SetIndex(pagesRange.Start + numVisible);
			++numVisible;
		}
		else
			pb.set_Visible(false);
	}

	while (numVisible < pagesRange.End - pagesRange.Start)
	{
		var a = document.createElement("a");
		this._pageButtonsElement.appendChild(a);
		var pb = new $RepeaterPageButtonNormal(a, this._repeater); // create new for pages added since render
		this._pageButtons.push(pb);
		this._AddButton(pb);
		pb.SetIndex(pagesRange.Start + numVisible);
		++numVisible;
	}

	for (var iButton = 0; iButton < this._allButtons.length; ++iButton)
	{
		this._allButtons[iButton].UpdateStyle();
		this._allButtons[iButton].UpdateStatus();
	}
}

